﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Peninsula.Utility.Extensions;

namespace Peninsula.MVC
{
    public abstract class BaseController : Controller
    {
        #region ActionResult 

        public JsonResult JsonAllowGet(object data)
        {
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        #endregion

        /// <summary>
        /// 判断是否手机浏览器
        /// </summary>
        //public bool IsMobile
        //{
        //    get
        //    {
        //        string agent = Request.UserAgent?.ToLower()?.Trim() ?? "";
        //        bool isMobile = Request.Browser.IsMobileDevice ||
        //            agent.IndexOf("android") != -1 ||
        //            agent.IndexOf("iphone") != -1 ||
        //            agent.IndexOf("samsung") != -1 ||
        //            agent.IndexOf("mobile") != -1 ||
        //            agent.IndexOf("mobi") != -1 ||
        //            agent.IndexOf("lg") != -1 ||
        //            agent.IndexOf("htc") != -1 ||
        //            agent.IndexOf("j2me") != -1 ||
        //            agent.IndexOf("ucweb") != -1 ||
        //            agent.IndexOf("opera mini") != -1 ||
        //            agent.IndexOf("nokia") != -1 ||
        //            agent.IndexOf("sonyericsson") != -1 ||
        //            agent.IndexOf("mot") != -1 ||
        //            agent.IndexOf("blackberry") != -1;

        //        string device = Request["device"];
        //        return device.IsNullOrWhiteSpace()
        //                            ? isMobile
        //                            : (device.ToLower() == "mobile")
        //              || IsWeChat;
        //    }
        //}

        //public bool IsWeChat
        //{
        //    get
        //    {
        //        return Request.UserAgent.IsNotNull() && Request.UserAgent.ToLower().Contains("micromessenger");
        //    }
        //}

        /// <summary>
        /// 获取文件实际地址
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileCategory">文件分类</param>
        /// <returns>文件实际地址</returns>
        protected virtual string GetPhysicalPath(string fileName, string filePath)
        {
            return Path.Combine(HttpContext.Server.MapPath(filePath),
                               Path.GetFileName(fileName));
        }

        /// <summary>
        /// 获取文件相对地址
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileCategory">文件分类</param>
        /// <returns>文件相对地址</returns>
        protected virtual string GetFileRelativePath(string fileName, string filePath)
        {
            return Path.Combine(filePath, Path.GetFileName(fileName));
        }
    }
}
